package com.example.module_hp_white_noise.fragment;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Switch;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.module_hp_white_noise.HpWhiteNoiseMainFragment;
import com.example.module_hp_white_noise.R;
import com.example.module_hp_white_noise.activity.HpWhiteNoiseChildListActivity;
import com.example.module_hp_white_noise.activity.HpWhiteNoiseChildPlayActivity;
import com.example.module_hp_white_noise.activity.HpWhiteNoiseFragment1ChildActivity;
import com.example.module_hp_white_noise.adapter.HpWhiteNoisePage1Adapter;
import com.example.module_hp_white_noise.adapter.HpWhiteNoisePage1Chunk2Adapter;
import com.example.module_hp_white_noise.base.BaseMvvmFragment;
import com.example.module_hp_white_noise.base.BaseUtils;
import com.example.module_hp_white_noise.base.BaseViewModel;
import com.example.module_hp_white_noise.databinding.FragmentHpWhiteNoisePage1Binding;
import com.example.module_hp_white_noise.entity.HpWhiteNoiseEntity1;
import com.example.module_hp_white_noise.entity.Music;
import com.example.module_hp_white_noise.service.MusicService;
import com.example.module_hp_white_noise.util.Utils;
import com.fwlst.lib_base.utils.AdUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.BIND_AUTO_CREATE;

public class HpWhiteNoiseFragment1 extends BaseMvvmFragment<FragmentHpWhiteNoisePage1Binding, BaseViewModel> implements View.OnClickListener{
    private JSONArray jsonArray;
    @Override
    protected int initLayout() {
        return R.layout.fragment_hp_white_noise_page1;
    }

    @Override
    protected int initBR() {
        return 0;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        //banner广告
        AdUtils.INSTANCE.loadBannerAd(mContext,binding.bannerContainer);
        //读取总json
        initJson();
        //块1数据
        initChunk1Data();
        //块2数据
        initChunk2Data();
        //块3数据
        initChunk3Data();
        //关联事件
        binding.page1ToChild1.setOnClickListener(this);
        binding.page1ToChild2.setOnClickListener(this);
        binding.page1ToChild3.setOnClickListener(this);
        binding.page1ToList1.setOnClickListener(this);
        binding.page1ToList2.setOnClickListener(this);
        binding.page1ToList3.setOnClickListener(this);
        binding.page1ToViewpage2.setOnClickListener(this);
        binding.page1ToViewpage3.setOnClickListener(this);
    }
    private void initJson(){
        String objString = BaseUtils.getJson("baizaoying.json",mContext);
        try {
            jsonArray = new JSONArray(objString);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    private void initChunk1Data(){
        HpWhiteNoisePage1Adapter hpWhiteNoisePage1Adapter = new HpWhiteNoisePage1Adapter();
        binding.fragment1Chunk1Rv.setLayoutManager(new GridLayoutManager(mContext,2));
        binding.fragment1Chunk1Rv.setAdapter(hpWhiteNoisePage1Adapter);
        List<HpWhiteNoiseEntity1> mDataList1 = new ArrayList<>();
        mDataList1.add(new HpWhiteNoiseEntity1(46,1,"noise_img46","","城市之声","多个声音一起听","减压,助眠"));
        mDataList1.add(new HpWhiteNoiseEntity1(47,1,"noise_img47","","大脑按摩","系列","助眠,愉快"));
        mDataList1.add(new HpWhiteNoiseEntity1(48,1,"noise_img48","","国外流行","系列","减压,助眠,改善"));
        mDataList1.add(new HpWhiteNoiseEntity1(49,1,"noise_img49","","梦回清宫","系列","改善,助眠"));
        hpWhiteNoisePage1Adapter.setNewData(mDataList1);
        hpWhiteNoisePage1Adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bd_item = new Bundle();
                bd_item.putInt("tags",mDataList1.get(position).getId());
                bd_item.putString("title",mDataList1.get(position).getTitle());
                bd_item.putString("imgUrl",mDataList1.get(position).getImgUrl());
                navigateToWithBundle(HpWhiteNoiseChildListActivity.class,bd_item);
            }
        });
    }
    private void initChunk2Data(){
        List<HpWhiteNoiseEntity1> mDataList2 = new ArrayList<>();
        HpWhiteNoisePage1Chunk2Adapter hpWhiteNoisePage1Chunk2Adapter = new HpWhiteNoisePage1Chunk2Adapter();
        binding.fragment1Chunk2Rv.setLayoutManager(new GridLayoutManager(mContext,2));
        binding.fragment1Chunk2Rv.setAdapter(hpWhiteNoisePage1Chunk2Adapter);
        hpWhiteNoisePage1Chunk2Adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bd_item = new Bundle();
                bd_item.putString("songUrl",mDataList2.get(position).getMusicUrl());
                bd_item.putString("imgUrl",mDataList2.get(position).getImgUrl());
                bd_item.putString("title",mDataList2.get(position).getTitle());
                navigateToWithBundle(HpWhiteNoiseChildPlayActivity.class,bd_item);
            }
        });
        List<Integer> indexArr = BaseUtils.genUniqueRandomVal(0,jsonArray.length()-1,6);
        if(jsonArray!=null){
            for (int i=0;i<indexArr.size();i++){
                try {
                    JSONObject jsonObject2 = (JSONObject) jsonArray.get(indexArr.get(i));
                    HpWhiteNoiseEntity1 hpWhiteNoiseEntity1 = new HpWhiteNoiseEntity1(indexArr.get(i),2,
                            "music_img"+(indexArr.get(i)% Utils.musicImgCount+1),
                            jsonObject2.getString("ossId"),
                            jsonObject2.getString("name"),
                            "",
                            ""
                    );
                    mDataList2.add(hpWhiteNoiseEntity1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        hpWhiteNoisePage1Chunk2Adapter.setNewData(mDataList2);
    }
    private void initChunk3Data(){
        List<HpWhiteNoiseEntity1> mDataList3 = new ArrayList<>();
        HpWhiteNoisePage1Adapter hpWhiteNoisePage1Adapter3 = new HpWhiteNoisePage1Adapter();
        binding.fragment1Chunk3Rv.setLayoutManager(new GridLayoutManager(mContext,2));
        binding.fragment1Chunk3Rv.setAdapter(hpWhiteNoisePage1Adapter3);
        hpWhiteNoisePage1Adapter3.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bd_item = new Bundle();
                bd_item.putString("songUrl",mDataList3.get(position).getMusicUrl());
                bd_item.putString("imgUrl",mDataList3.get(position).getImgUrl());
                bd_item.putString("title",mDataList3.get(position).getTitle());
                navigateToWithBundle(HpWhiteNoiseChildPlayActivity.class,bd_item);
            }
        });
        List<Integer> indexArr = BaseUtils.genUniqueRandomVal(0,jsonArray.length()-1,6);
        if(jsonArray!=null){
            for (int i=0;i<indexArr.size();i++){
                try {
                    JSONObject jsonObject2 = (JSONObject) jsonArray.get(indexArr.get(i));
                    HpWhiteNoiseEntity1 hpWhiteNoiseEntity1 = new HpWhiteNoiseEntity1(indexArr.get(i),2,
                            "music_img"+(indexArr.get(i)% Utils.musicImgCount+1),
                            jsonObject2.getString("ossId"),
                            jsonObject2.getString("name"),
                            "",
                            null
                    );
                    mDataList3.add(hpWhiteNoiseEntity1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        hpWhiteNoisePage1Adapter3.setNewData(mDataList3);
    }
    //点击事件
    @Override
    public void onClick(View v) {
        Bundle bd_item = new Bundle();
        if (v.getId() == R.id.page1_to_child1 || v.getId() == R.id.page1_to_child3) {
            bd_item.putInt("type",1);
            navigateToWithBundle(HpWhiteNoiseFragment1ChildActivity.class,bd_item);
        }else if(v.getId() == R.id.page1_to_child2){
            bd_item.putInt("type",2);
            navigateToWithBundle(HpWhiteNoiseFragment1ChildActivity.class,bd_item);
        }else if(v.getId() == R.id.page1_to_viewpage2 || v.getId() == R.id.page1_to_viewpage3){
            HpWhiteNoiseMainFragment hpWhiteNoiseMainFragment = (HpWhiteNoiseMainFragment) getParentFragment();
            hpWhiteNoiseMainFragment.setViewPageItem(v.getId() == R.id.page1_to_viewpage2?1:3);
        }else if(v.getId() == R.id.page1_to_list1){
            bd_item.putInt("tags",45);
            bd_item.putString("title","美好时光");
            bd_item.putString("imgUrl","noise_img45");
            navigateToWithBundle(HpWhiteNoiseChildListActivity.class,bd_item);
        }else if(v.getId() == R.id.page1_to_list2){
            bd_item.putInt("tags",35);
            bd_item.putString("title","深夜惊醒");
            bd_item.putString("imgUrl","noise_img35");
            navigateToWithBundle(HpWhiteNoiseChildListActivity.class,bd_item);
        }else if(v.getId() == R.id.page1_to_list3){
            bd_item.putInt("tags",36);
            bd_item.putString("title","静候佳音");
            bd_item.putString("imgUrl","noise_img36");
            navigateToWithBundle(HpWhiteNoiseChildListActivity.class,bd_item);
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        AdUtils.INSTANCE.destroyBannerAd();
    }
}
